package com.demo.controller;

import com.demo.dao.MajorMapper;
import com.demo.dao.ManagerMapper;
import com.demo.dao.StudentMapper;
import com.demo.dao.TeacherMapper;
import com.demo.entity.Major;
import com.demo.entity.Manager;
import com.demo.entity.Student;
import com.demo.entity.Teacher;
import com.demo.util.ExcelUtil;
import com.demo.util.Md5;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 67380 on 2016/9/26.
 */
@Controller
@RequestMapping("/")
@SessionAttributes({"user","role"})
public class SysController {
    public static int stuopty=1;//研究生系统 0-关闭 1-开启
    public static int teaopty=1;//导师系统
    public static int colopty=1;//负责人系统
    @Resource
    ManagerMapper managerDao;

    @Resource
    StudentMapper studentDao;

    @Resource
    TeacherMapper teacherDao;

    @Resource
    MajorMapper majorDao;

    @RequestMapping("/login")
    public String login(Model model,String uname,String pwd,int role){

        if(role==0){
            Manager manager=managerDao.logincheck(uname, Md5.Md5(pwd),0);
            if(manager==null){
                model.addAttribute("loginerror","账号或密码错误");
                return "forward:/";
            }
            model.addAttribute("user",manager);
            model.addAttribute("role",0);

        }
        else if(role==1){
            if(colopty==0){
                model.addAttribute("loginerror","该系统已关闭");
                return "forward:/";
            }

            Manager manager=managerDao.logincheck(uname, Md5.Md5(pwd),1);
            if(manager==null){
                model.addAttribute("loginerror","账号或密码错误");
                return "forward:/";
            }
            model.addAttribute("user",manager);
            model.addAttribute("role",1);

        }
        else if(role==2){
            if(stuopty==0){
                model.addAttribute("loginerror","该系统已关闭");
                return "forward:/";
            }

            Student student=studentDao.logincheck(uname, Md5.Md5(pwd));
            if(student==null){
                model.addAttribute("loginerror","账号或密码错误");
                return "forward:/";
            }
            model.addAttribute("user",student);
            model.addAttribute("role",2);
        }
        else{
            if(teaopty==0){
                model.addAttribute("loginerror","该系统已关闭");
                return "forward:/";
            }

            Teacher teacher=teacherDao.logincheck(uname, Md5.Md5(pwd));
            if(teacher==null){
                model.addAttribute("loginerror","账号或密码错误");
                return "forward:/";
            }
            model.addAttribute("user",teacher);
            model.addAttribute("role",3);
        }

        return "main";
    }


    @RequestMapping("/stumanager")
    public String stumanager(Model model){
        List<Student> studentList=studentDao.selectAllWithMajor();

        model.addAttribute("studentList",studentList);
        return "sys/stumain";
    }

    @RequestMapping("/studelete")
    public String studelete(String cardid){

        studentDao.deleteByPrimaryKey(cardid);
        return "redirect:/stumanager";
    }

    @RequestMapping("/studetail")
    public String stuedit(Model model,String cardid){
        Student student=studentDao.selectStuWithMajor(cardid);
        List<Major> majorList=majorDao.selectAll();
        model.addAttribute("majorList",majorList);
        model.addAttribute("stu",student);
        return "sys/studetail";
    }

    @RequestMapping("/studupdate")
    public String studupdate(Model model,Student student){
        studentDao.updateByPrimaryKeySelective(student);
        List<Major> majorList=majorDao.selectAll();
        model.addAttribute("majorList",majorList);
        model.addAttribute("stu",student);
        return "sys/studetail";
    }
    @ResponseBody
    @RequestMapping("/studReset")
    public String studReset(Model model,String sid){
        Student student=new Student();
        student.setSid(sid);
        student.setSpwd(Md5.Md5(sid.substring(sid.length()-6,sid.length())));
        String i=studentDao.updateByPrimaryKeySelective(student)+"";

        return i;
    }

    @RequestMapping("/studentexcel")
    public String studentexcel(Model model, MultipartFile file) throws IOException {
        List<Student> studentList=ExcelUtil.readStudentXSSFAllRows(new XSSFWorkbook(file.getInputStream()));
        for (Student s:studentList) {
            try {
                studentDao.insertSelective(s);
            }catch (Exception e){
                studentDao.updateByPrimaryKeySelective(s);
            }

        }
        model.addAttribute("studentList",studentList);
        return "sys/stumain";
    }

    @RequestMapping("/teamanager")
    public String teamanager(Model model){
        List<Teacher> teacherList=teacherDao.selectAllWithMajor();
        model.addAttribute("teacherList",teacherList);
        return "sys/teamain";
    }

    @RequestMapping("/teadelete")
    public String teadelete(String cardid){

        teacherDao.deleteByPrimaryKey(cardid);
        return "redirect:/teamanager";
    }

    @RequestMapping("/teadetail")
    public String teaedit(Model model,String cardid){
        Teacher teacher=teacherDao.selectStuWithMajor(cardid);
        List<Major> majorList=majorDao.selectAll();
        model.addAttribute("majorList",majorList);
        model.addAttribute("tea",teacher);
        return "sys/teadetail";
    }

    @RequestMapping("/teaupdate")
    public String teaupdate(Model model,Teacher teacher){
        teacherDao.updateByPrimaryKeySelective(teacher);
        List<Major> majorList=majorDao.selectAll();
        model.addAttribute("majorList",majorList);
        model.addAttribute("tea",teacher);
        return "sys/teadetail";
    }
    @ResponseBody
    @RequestMapping("/teaReset")
    public String teaReset(Model model,String sid){
        Teacher teacher=new Teacher();
        teacher.setTid(sid);
        teacher.setTpwd(Md5.Md5("888888"));
        String i=teacherDao.updateByPrimaryKeySelective(teacher)+"";

        return i;
    }

    @RequestMapping("/teaadd")
    public String teaadd(Model model){
        List<Major> majorList=majorDao.selectAll();
        model.addAttribute("majorList",majorList);
        return "sys/teaadd";
    }
    @RequestMapping("/teainsert")
    public String teainsert(Model model,Teacher teacher){
        try{
            teacher.setTpwd(Md5.Md5("888888"));
            teacherDao.insertSelective(teacher);
        }catch (Exception e){
            List<Major> majorList=majorDao.selectAll();
            model.addAttribute("majorList",majorList);
            model.addAttribute("error","该工号已存在");
            model.addAttribute("tea",teacher);
            return "sys/teaadd";
        }

        return "redirect:/teamanager";
    }

    @RequestMapping("/teaexcel")
    public String teaexcel(Model model, MultipartFile file) throws IOException {
        List<Teacher> teacherList=ExcelUtil.readTeacherXSSFAllRows(new XSSFWorkbook(file.getInputStream()));
        for (Teacher t:teacherList) {
            try {
                teacherDao.insertSelective(t);
            }catch (Exception e){
                teacherDao.updateByPrimaryKeySelective(t);
            }
        }
        model.addAttribute("teacherList",teacherList);
        return "sys/teamain";
    }

    @RequestMapping("/majormanager")
    public String majormanager(Model model){
        List<Major> majorList=majorDao.selectAll();

        model.addAttribute("majorList",majorList);
        return "sys/majormain";
    }

    @RequestMapping("/majorexcel")
    public String majorexcel(Model model, MultipartFile file) throws IOException {
        List<Major> majorList=ExcelUtil.readMajorXSSFAllRows(new XSSFWorkbook(file.getInputStream()));
        for (Major m:majorList) {
            try {
                majorDao.insertSelective(m);
            }catch (Exception e){
                majorDao.updateByPrimaryKeySelective(m);
            }

        }
        model.addAttribute("majorList",majorList);
        return "sys/majormain";
    }

    @RequestMapping("/majordelete")
    public String majordelete(String cardid){

        majorDao.deleteByPrimaryKey(cardid);
        return "redirect:/majormanager";
    }

    @RequestMapping("/majordetail")
    public String majordetail(Model model,String cardid){
        Major major=majorDao.selectByPrimaryKey(cardid);


        model.addAttribute("major",major);
        return "sys/majordetail";
    }

    @RequestMapping("/majorupdate")
    public String majorupdate(Model model,Major major){
        majorDao.updateByPrimaryKeySelective(major);


        model.addAttribute("major",major);
        return "sys/majordetail";
    }

    @RequestMapping("/majoradd")
    public String majoradd(Model model){

        return "sys/majoradd";
    }
    @RequestMapping("/majorinsert")
    public String majorinsert(Model model,Major major){
        try{
            majorDao.insertSelective(major);
        }catch (Exception e){
            model.addAttribute("error","该编号已存在");
            model.addAttribute("major",major);
            return "sys/majoradd";
        }

        return "redirect:/majormanager";
    }

    @RequestMapping("/colmanager")
    public String colmanager(Model model){
        List<Manager> managerList=managerDao.selectAll();
        model.addAttribute("managerList",managerList);
        return "sys/colmain";
    }

    @RequestMapping("/coldelete")
    public String coldelete(String cardid){

        managerDao.deleteByPrimaryKey(cardid);
        return "redirect:/colmanager";
    }

    @RequestMapping("/coldetail")
    public String coldetail(Model model,String cardid){
        Manager manager=managerDao.selectByPrimaryKey(cardid);
        List<Major> majorList=majorDao.selectCollege();
        model.addAttribute("majorList",majorList);
        model.addAttribute("col",manager);
        return "sys/coldetail";
    }

    @RequestMapping("/colupdate")
    public String colupdate(Model model,Manager manager){
        managerDao.updateByPrimaryKeySelective(manager);
        List<Major> majorList=majorDao.selectCollege();
        model.addAttribute("majorList",majorList);
        model.addAttribute("col",manager);
        return "sys/coldetail";
    }
    @ResponseBody
    @RequestMapping("/colReset")
    public String colReset(Model model,String sid){
        Manager manager=new Manager();
        manager.setMname(sid);
        manager.setMpwd(Md5.Md5("888888"));
        String i=managerDao.updateByPrimaryKeySelective(manager)+"";
        return i;
    }

    @RequestMapping("/coladd")
    public String coladd(Model model){
        List<Major> majorList=majorDao.selectCollege();
        model.addAttribute("majorList",majorList);
        return "sys/coladd";
    }
    @RequestMapping("/colinsert")
    public String colinsert(Model model,Manager manager){
        try{
            manager.setMrole(1);
            manager.setMpwd("888888");
            managerDao.insertSelective(manager);
        }catch (Exception e){
            model.addAttribute("error","该编号已存在");
            List<Major> majorList=majorDao.selectCollege();
            model.addAttribute("majorList",majorList);
            return "sys/coladd";
        }

        return "redirect:/colmanager";
    }

    @RequestMapping("/sys")
    public String sys(Model model){

        model.addAttribute("stu",stuopty);
        model.addAttribute("tea",teaopty);
        model.addAttribute("col",colopty);
        return "sys/sysmana";
    }

    @RequestMapping("/sysupdate")
    public String sysupdate(Model model,int optname,int opt){
        if(optname==1){
            stuopty=opt;
        }
        else if(optname==2){
            teaopty=opt;
        }
        else if(optname==3){
            colopty=opt;
        }
        model.addAttribute("stu",stuopty);
        model.addAttribute("tea",teaopty);
        model.addAttribute("col",colopty);
        return "sys/sysmana";
    }

    @RequestMapping("/test1")
    public String test1(Model model){

        return "main";
    }
}
